﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace Expense.DataLayer
{
    //implements business methods
    public partial class AdminSetting
    {

        #region methods;     

        /// <summary>
        /// Inserts an AdminSettingItem row.
        /// </summary>
        /// <param name="AdminSettingItem">An AdminSettingItem object.</param>
        /// 
        public AdminSetting Insert()
        {
            AdminSetting AdminSettingItem=this;
            using (ExpenseDataDataContext dc = new ExpenseDataDataContext())
            {
                // Insert the values. Datacontext will be attached here.
                AdminSettingItem.DateSubmitted = DateTime.Now;
                dc.AdminSettings.InsertOnSubmit(AdminSettingItem);
                try
                {
                    dc.SubmitChanges();
                    return AdminSettingItem;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        
        /// <summary>
        /// Updates an AdminSettingItem row.
        /// </summary>
        /// <param name="AdminSettingItem">A AdminSettingItem object.</param>
        public AdminSetting Update()
        {
            AdminSetting newAdminSettingItem = this;
            using (ExpenseDataDataContext dc = new ExpenseDataDataContext())
            {
                try
                {
                    AdminSetting oldAdminSetting =
                (from j in dc.AdminSettings
                 where j.SettingName.ToLower() == newAdminSettingItem.SettingName.ToLower()
                 select j).FirstOrDefault();

                    if (oldAdminSetting == null)
                    {
                        // Insert the values. Datacontext will be attached here.
                        oldAdminSetting = this;
                        oldAdminSetting.DateSubmitted = DateTime.Now;
                        dc.AdminSettings.InsertOnSubmit(oldAdminSetting);
                    }
                    else
                    { 
                        //update
                        this.Copy(oldAdminSetting, newAdminSettingItem);
                        oldAdminSetting.DateModified = DateTime.Now;
                    }
                    dc.SubmitChanges();
                    return oldAdminSetting;
                }               
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


        private AdminSetting Copy( AdminSetting oldAdminSetting, AdminSetting newAdminSetting)
        {
            oldAdminSetting.SettingName = newAdminSetting.SettingName;
            oldAdminSetting.SettingValue = newAdminSetting.SettingValue;
            oldAdminSetting.ModifiedBy = newAdminSetting.ModifiedBy;
            oldAdminSetting.DateModified = newAdminSetting.DateModified;

            return oldAdminSetting;

        }


        public bool Delete(int ID)
        {
            try
            {
                using (ExpenseDataDataContext db = new ExpenseDataDataContext())
                {
                    var deletedItem =
                    (from t in db.AdminSettings 
                     where t.ID == ID
                     select t).SingleOrDefault();
                    if (deletedItem != null)
                    {
                        db.AdminSettings.DeleteOnSubmit(deletedItem);                          
                        db.SubmitChanges();
                    }
                }
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }

        public AdminSetting Get(int ID)
        {
            ExpenseDataDataContext db = new ExpenseDataDataContext();
            AdminSetting itemQuery =
       (from t in db.AdminSettings
        where t.ID == ID
        select t).SingleOrDefault();
            return itemQuery;
        }

        public AdminSetting Get(string settingName)
        {
            ExpenseDataDataContext db = new ExpenseDataDataContext();
            AdminSetting itemQuery =
       (from t in db.AdminSettings
        where t.SettingName.ToLower() == settingName.ToLower() 
        select t).FirstOrDefault();
            return itemQuery;
        }


        public List<AdminSetting> List()
        {
            ExpenseDataDataContext db = new ExpenseDataDataContext();
            var itemQuery =
       (from t in db.AdminSettings
        select t).ToList();
            return itemQuery;
        }


        #endregion
        
    }
}
